﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using ExplorerPlugin.Properties;

namespace ExplorerPlugin
{
    #region Enums

    public enum ListviewButtonState { Normal, Hovered, Clicked, Focussed, Disabled }

    #endregion

    #region Classes

    public class ListviewButton : Panel
    {
        #region Contructor

        public ListviewButton() : this(new Point(0), new Size(0, 0)) { }

        public ListviewButton(Point ini_location) : this(ini_location, new Size(0, 0)) { }

        public ListviewButton(Point ini_location, Size ini_size)
        {
            Initiatize();

            g = this.CreateGraphics();
        }

        public void Initiatize()
        {
            this.lbl = new Label();
            //this.lbl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lbl.AutoSize = true;
            this.lbl.BackColor = Color.Transparent;
            
            this.pbx = new PictureBox();
            this.pbx.SizeMode = PictureBoxSizeMode.StretchImage;
            //this.pbx.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lbl.BackColor = Color.Transparent;

            // set image related attributes
            this.BackColor = Color.Transparent;
            this.BackgroundImage = ExplorerPlugin.Properties.Resources.listviewButton;
            this.BackgroundImageLayout = ImageLayout.Stretch;
            this.Location = new Point(0, 500);
            this.Size = new Size(241, 103);
            this.Icon = ExplorerPlugin.Properties.Resources.fileIconText;
            this.Text = "Nothing";

            this.Controls.Add(this.lbl);
            this.Controls.Add(this.pbx);

            // set events
            SizeChanged += new EventHandler(panel_sizeChanged);
        }

        #endregion

        #region Private Attributes

        Graphics g;

        static Image normal, hovered, clicked, focussed, disabled, error;
        Label lbl;
        PictureBox pbx;
        ListviewButtonState currentState;

        #endregion

        #region Public Attributes

        public Image ImageNormal
        {
            get { return normal; }
            set { normal = value; }
        }

        public Image ImageHovered
        {
            get { return hovered; }
            set { hovered = value; }
        }

        public Image ImageClicked
        {
            get { return clicked; }
            set { clicked = value; }
        }

        public Image ImageFocussed
        {
            get { return focussed; }
            set { focussed = value; }
        }

        public Image ImageDisabled
        {
            get { return disabled; }
            set { disabled = value; }
        }

        public Image Icon
        {
            get { return pbx.Image; }
            set
            {
                pbx.Image = value;
                this.Refresh();
            }
        }

        public ListviewButtonState CurrentState
        {
            get { return currentState; }
        }

        public override String Text
        {
            get { return lbl.Text; }
            set { lbl.Text = value; }
        }

        #endregion

        #region Private Eventhandlers

        private void panel_sizeChanged(object sender, EventArgs e)
        {
            this.Size = new Size((int)(this.Size.Height * 1.19), this.Size.Height);

            pbx.Size = new Size(this.Size.Height - (int)(.3 * this.Size.Height), this.Size.Height - (int)(.3 * this.Size.Height));
            pbx.Location = new Point((int)((this.Size.Width - pbx.Width) / 2), /**/(int)(.1 * this.Size.Height)/**/);

            lbl.Font = new Font(lbl.Font.Name, .1f * this.Size.Height, lbl.Font.Style, lbl.Font.Unit, lbl.Font.GdiCharSet, lbl.Font.GdiVerticalFont);
            lbl.Location = new Point((int)((this.Size.Width - g.MeasureString(lbl.Text, lbl.Font).Width) / 2), pbx.Location.Y + pbx.Size.Height);
        }

        #endregion

        #region Public Functions

        public void LoadImages()
        {
            // Load all images from Layout Dir
            normal = new Bitmap(Directory.GetCurrentDirectory() + "\\Layout\\listviewButton.png");
            hovered = new Bitmap(Directory.GetCurrentDirectory() + "\\Layout\\listviewButtonHover.png");
            clicked = new Bitmap(Directory.GetCurrentDirectory() + "\\Layout\\listviewButtonClick.png");
            focussed = new Bitmap(Directory.GetCurrentDirectory() + "\\Layout\\listviewButtonFocus.png");
            disabled = new Bitmap(Directory.GetCurrentDirectory() + "\\Layout\\listviewButtonDisabled.png");
            error = new Bitmap(Directory.GetCurrentDirectory() + "\\Layout\\listviewButtonError.png");

            OnSizeChanged(null);
        }



        public void UpdateImage()
        {
            switch (currentState)
            {
                case ListviewButtonState.Normal:
                    BackgroundImage = normal;
                    break;
                case ListviewButtonState.Hovered:
                    BackgroundImage = hovered;
                    break;
                case ListviewButtonState.Clicked:
                    BackgroundImage = clicked;
                    break;
                case ListviewButtonState.Focussed:
                    BackgroundImage = focussed;
                    break;
                case ListviewButtonState.Disabled:
                    BackgroundImage = disabled;
                    break;
                default:
                    BackgroundImage = error;
                    break;
            }
        }

        #endregion

        #region Private Functions

        private void changeState(ListviewButtonState newState)
        {
            currentState = newState;
            UpdateImage();
        }

        #endregion
    }

    #endregion
}